﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;

namespace UserInterface
{
    using BusinessLogicLayer;
    using BusinessObjectLayer;
    using ComplexDataType;
    using JntMsgBox;
    using System.Net;
    using System.IO;

    public partial class EditCategoryItemUI : Form
    {
        private CategoryItemBLL _CategoryItemBLL;
        private CategoryItemBOL _CategoryItemBOL;
        private ResourceManager _ResourceManager = new ResourceManager("UserInterface.Properties.Resources", Assembly.GetExecutingAssembly());
        ExceptionHandlerBOL _ExceptionHandlerBOL;
        private FormMode formMode;
        string UpdateText;
        string DeleteText;
        string UpdateConfirmation;
        string DeleteConfirmation;

        public EditCategoryItemUI()
        {
            InitializeComponent();
            editButton.Text = _ResourceManager.GetString("CreateButtonText");
            this.Text = _ResourceManager.GetString("AddNewCategoryItem");
            categoryItemTitleTextBox.Focus();
            deleteButton.Visible = false;
            formMode = FormMode.Insert;
            _CategoryItemBLL = new CategoryItemBLL();
            CategoryComboBoxBind();
        }
        public EditCategoryItemUI(CategoryItemBOL categoryItemBOL)
        {
            InitializeComponent();
            formMode = FormMode.Update;
            categoryItemTitleTextBox.Focus();
            _CategoryItemBLL = new CategoryItemBLL();
            _CategoryItemBOL = categoryItemBOL;
            CategoryComboBoxBind();
            fillTextBoxes(categoryItemBOL);            
        }
        private void SetResourceManager()
        {
            try
            {
                UpdateText = _ResourceManager.GetString("Update");
                DeleteText = _ResourceManager.GetString("Delete");
                UpdateConfirmation = _ResourceManager.GetString("UpdateConfirmation");
                DeleteConfirmation = _ResourceManager.GetString("DeleteConfirmation");
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                noticeLabel.ForeColor = Color.Red;
                noticeLabel.Text = _ExceptionHandlerBOL.Title;
            }
        }
      
        private void fillTextBoxes(CategoryItemBOL _CategoryItemBOL)
        {
            categoryItemIDTextBox.Text = _CategoryItemBOL.CategoryItemId.ToString();
            categoryItemTitleTextBox.Text = _CategoryItemBOL.CategoryItemTitle;
            descriptionTextBox.Text = _CategoryItemBOL.Description;
            ListItem _ListItem = new ListItem(_CategoryItemBOL.CategoryId, _CategoryItemBOL.CategoryTitle);
            categoryComboBox.SelectedIndex = ComboBoxSelectedIndex(categoryComboBox, _ListItem);
        }
        private int ComboBoxSelectedIndex(ComboBox _ComboBox, ListItem _ListItem)
        {
            for (int index = 0; index < _ComboBox.Items.Count; index++)
            {
                if (((ListItem)_ComboBox.Items[index]).Id == _ListItem.Id)
                    return index;
            }
            return -1;
        }
        private void CategoryComboBoxBind()
        {
            try
            {
                CategoryBOL[] CategoryRecords = new CategoryBLL().SelectComboBox();
                categoryComboBox.Items.Clear();
                if (CategoryRecords != null)
                    for (int index = 0; index < CategoryRecords.Length; index++)
                    {
                        categoryComboBox.Items.Add(new ListItem(CategoryRecords[index].CategoryId, CategoryRecords[index].CategoryTitle));
                    }
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                noticeLabel.ForeColor = Color.Red;
                noticeLabel.Text = _ExceptionHandlerBOL.Title;
            }
        }
        private void cancelButton_Click(object sender, EventArgs e)
        {
            CloseForm();
        }
        private void CloseForm()
        {
            this.Close();
        }
        private void editButton_Click(object sender, EventArgs e)
        {
            if (ValidateItem())
                GetEditConfirmtion();
        }
        private bool ValidateItem()
        {
            bool isValid = true;
            if (categoryItemTitleTextBox.Text == string.Empty)
            {
                reqErrorProvider.SetError(categoryItemTitleTextBox, "*");
                isValid = false;
            }           
            if (descriptionTextBox.Text == string.Empty)
            {
                reqErrorProvider.SetError(descriptionTextBox, "*");
                isValid = false;
            }
            if (categoryComboBox.SelectedIndex <=0)
            {
                reqErrorProvider.SetError(categoryComboBox, "*");
                isValid = false;
            }
            return isValid;
        }
        private void GetEditConfirmtion()
        {
            try
            {
                if (formMode == FormMode.Update)
                {
                    SetResourceManager();
                    JntMsgBoxFarsi _JntMsgBoxFarsi = new JntMsgBoxFarsi();
                    DialogResult result = _JntMsgBoxFarsi.Show(UpdateConfirmation, UpdateText, JntStyle.YesNo);
                    if (result == DialogResult.No)
                        return;
                }
                Edit();
                CloseForm();
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                noticeLabel.ForeColor = Color.Red;
                noticeLabel.Text = _ExceptionHandlerBOL.Title;
            }
            finally { }
        }
        private void Edit()
        {
            short? categoryItemId = null;
            try
            {
                categoryItemId = Convert.ToInt16(categoryItemIDTextBox.Text);
            }
            catch
            {
            }
            string categoryItemTitle = categoryItemTitleTextBox.Text;
            string description = descriptionTextBox.Text;
            short?  categoryId = ((ListItem)categoryComboBox.SelectedItem).Id;
            CategoryItemBOL _CategoryItemBOL = new CategoryItemBOL(categoryItemId, categoryItemTitle, description,categoryId,null);
            if (formMode == FormMode.Insert)
                _CategoryItemBLL.Insert(_CategoryItemBOL);
            else
                _CategoryItemBLL.Update(_CategoryItemBOL);
        }
        private void deleteButton_Click(object sender, EventArgs e)
        {
            GetDeleteConfirmtion();
        }
        private void GetDeleteConfirmtion()
        {
            try
            {
                SetResourceManager();
                JntMsgBoxFarsi _JntMsgBoxFarsi = new JntMsgBoxFarsi();
                DialogResult result = _JntMsgBoxFarsi.Show(DeleteConfirmation, DeleteText, JntStyle.YesNo);
                if (result == DialogResult.No)
                    return;
                _CategoryItemBLL.Delete(_CategoryItemBOL);
                CloseForm();
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                noticeLabel.ForeColor = Color.Red;
                noticeLabel.Text = _ExceptionHandlerBOL.Title;
            }
            finally { }
        }
        private void noticeLabel_Click(object sender, EventArgs e)
        {
            if (_ExceptionHandlerBOL != null)
                new ExceptionHandlerUI(_ExceptionHandlerBOL).ShowDialog();
            _ExceptionHandlerBOL = null;
        }
        private void setFarsiKeyboard_Enter(object sender, EventArgs e)
        {
            try
            {
                KeyboardControler.SetFarsiKeyboard();
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                noticeLabel.ForeColor = Color.Red;
                noticeLabel.Text = _ExceptionHandlerBOL.Title;
            }
        }    
    }
}